export const Second = 1
export const Minute = Second * 60
export const Hour = Minute * 60
export const Day = Hour * 24

export function getSeconds(date) {
  let seconds = 0

  seconds += date.getHours() * Hour
  seconds += date.getMinutes() * Minute
  seconds += date.getSeconds()

  return seconds
}

export function formatter(num) {
  return num < 10 ? `0${num}` : `${num}`
}

export function toTime(seconds) {
  const hours = Math.floor(seconds / Hour)
  seconds -= Hour * hours

  const minutes = Math.floor(seconds / Minute)
  seconds -= Minute * minutes

  return `${formatter(hours)}:${formatter(minutes)}:${formatter(seconds)}`
}

export function toRange(beginSeconds, endSeconds) {
  return `${toTime(beginSeconds)} - ${toTime(endSeconds)}`
}

export function sortRanges(timeRanges) {
  return timeRanges.sort((firstRange, secondRange) => {
    return firstRange.beginSeconds - secondRange.endSeconds
  })
}

export function rangeCheck(timeRange) {
  const { beginSeconds, endSeconds } = timeRange

  if (beginSeconds >= endSeconds) throw new Error('起始时间必须小于结束时间')
}

export function overlapCheck(timeRanges, timeRange) {
  const isInBetween = (beginSeconds, endSeconds, seconds) => {
    // debugger
    return seconds >= beginSeconds && seconds < endSeconds
  }

  const backup = sortRanges(timeRanges.concat())

  for (const range of timeRanges) {
    if (timeRange.beginSeconds === range.beginSeconds) { throw new Error('存在相同的起始时间') }
  }

  let isOverlap = false

  isOverlap = backup.some((range) => {
    const { beginSeconds, endSeconds } = range
    return (
      isInBetween(beginSeconds, endSeconds, timeRange.beginSeconds) ||
      isInBetween(beginSeconds, endSeconds, timeRange.endSeconds)
    )
  })
  if (isOverlap) throw new Error('时间段冲突')

  isOverlap = backup.some((range) => {
    const { beginSeconds, endSeconds } = timeRange
    return isInBetween(beginSeconds, endSeconds, range.beginSeconds)
  })
  if (isOverlap) throw new Error('你选择的时间段内已有其他班次')
}

export function buildAvailableTimeRanges(timeRangesOccupied) {
  if (!Array.isArray(timeRangesOccupied) || timeRangesOccupied.length <= 0) { return [{ beginSeconds: 0, endSeconds: Day - 1 }] }

  const backup = sortRanges(timeRangesOccupied.concat())

  const availableTimeRanges = []
  const minSeconds = 0
  const maxSeconds = Day

  const addRange = (beginSeconds, endSeconds) => {
    availableTimeRanges.push({ beginSeconds, endSeconds })
  }

  const len = backup.length
  // debugger
  for (let i = -1; i < len; i++) {
    let beginSeconds = null
    let endSeconds = null

    if (i === -1) {
      endSeconds = minSeconds
      beginSeconds = backup.at(i + 1).beginSeconds
    } else if (i >= (len - 1)) {
      endSeconds = backup.at(i).endSeconds
      beginSeconds = maxSeconds
    } else {
      endSeconds = backup.at(i).endSeconds
      beginSeconds = backup.at(i + 1).beginSeconds
    }

    if ((beginSeconds - endSeconds) <= 0) continue

    addRange(endSeconds, beginSeconds - 1)
  }

  return availableTimeRanges
}
